package com.ilovepostcard.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public abstract class PostcardTabView extends LinearLayout {


    public PostcardTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
    }

    public PostcardTabView(Context context) {
        super(context);
        inflate();
    }
    
    private void inflate() {
        View view = View.inflate(getContext(), getLayout(), this);
        int[] ids = getTabIds();
        int count = ids.length;
        final PostcardTabItem[] items = new PostcardTabItem[count];
        for (int i = 0; i < count; i++) {
            PostcardTabItem item = (PostcardTabItem) view.findViewById(ids[i]);
            items[i] = item;
        }
        ids = getContentIds();
        count = ids.length;
        final View[] contents = new View[count];
        for (int i = 0; i < count; i++) {
            contents[i] = view.findViewById(ids[i]);
        }
        
        for (int i = 0; i < count; i++) {
            final PostcardTabItem item = items[i];
            final int index = i;
            item.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    for (int j = 0; j < contents.length; j++) {
                        items[j].setHighlight(false);
                        contents[j].setVisibility(View.GONE);
                    }
                    items[index].setHighlight(true);
                    contents[index].setVisibility(View.VISIBLE);
                }
            });
        }
    }

    protected abstract int getLayout();
    
    protected abstract int[] getTabIds();
    
    protected abstract int[] getContentIds();
}
